package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltMonthlyAverageScoreVO;
import com.jwds.digital.bios.business.service.service.belt.IBeltHealthScoreDayService;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltHealthScoreDayReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltHealthScoreDayRespVO;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltHealthScoreDayAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltHealthScoreDay;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltHealthScoreDayMapper;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 皮带健康每天评分表实现类
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class BeltHealthScoreDayServiceImpl extends ServiceImpl<BeltHealthScoreDayMapper, BeltHealthScoreDay> implements IBeltHealthScoreDayService {

  @Autowired
  private BeltHealthScoreDayMapper baseMapper;

  @Override
  public BeltHealthScoreDayRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    BeltHealthScoreDay entity = baseMapper.selectById(id);
    return BeltHealthScoreDayAssembler.entity2dto(entity);
  }

  @Override
  public PageResponse<BeltHealthScoreDayRespVO> page(PageRequest<BeltHealthScoreDayReqDTO> request) {
    BeltHealthScoreDayReqDTO condition = request.getQueryCondition();
    Page<BeltHealthScoreDayRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
    IPage<BeltHealthScoreDayRespVO> pageData = baseMapper.pageQuery(page, condition);
    return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
  }

  @Override
  public List<BeltHealthScoreDayRespVO> list(BeltHealthScoreDayReqDTO reqDto) {
    return baseMapper.queryList(reqDto);
  }

  @Override
  public void add(BeltHealthScoreDayReqDTO reqDto) {
    BeltHealthScoreDay entity = BeltHealthScoreDayAssembler.dto2entity(reqDto);
    baseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchAdd(List<BeltHealthScoreDayReqDTO> dtoList) {
    dtoList.forEach(this::add);
  }

  @Override
  public void update(BeltHealthScoreDayReqDTO reqDto) {
    BeltHealthScoreDay oldEntity = baseMapper.selectById(reqDto.getId());
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    BeltHealthScoreDay newEntity = BeltHealthScoreDayAssembler.dto2entity(reqDto, oldEntity);
    baseMapper.updateById(newEntity);
  }

  @Override
  public void delete(Long id) {
    BeltHealthScoreDay oldEntity = baseMapper.selectById(id);
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    baseMapper.deleteById(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchDelete(List<Long> ids) {
    for (Long id : ids) {
      delete(id);
    }
  }

  @Override
  public Integer todayMechanicalScore(Long id) {
    LocalDateTime todayStart = LocalDate.now().atStartOfDay();
    BeltHealthScoreDay beltHealthScoreDay = baseMapper.selectOne(new LambdaQueryWrapper<BeltHealthScoreDay>()
            .eq(BeltHealthScoreDay::getHealthType, 0)
                    .eq(BeltHealthScoreDay::getBeltId, id)
            .between(BeltHealthScoreDay::getScoreDay, todayStart, todayStart.plusDays(1)));
    return beltHealthScoreDay == null ? -1 : beltHealthScoreDay.getScore().intValue();
  }

  @Override
  public Integer todayFiveDefenseScore(Long id) {
    LocalDateTime todayStart = LocalDate.now().atStartOfDay();
    BeltHealthScoreDay beltHealthScoreDay = baseMapper.selectOne(new LambdaQueryWrapper<BeltHealthScoreDay>()
            .eq(BeltHealthScoreDay::getHealthType, 2)
                    .eq(BeltHealthScoreDay::getBeltId, id)
            .between(BeltHealthScoreDay::getScoreDay, todayStart, todayStart.plusDays(1)));
    return beltHealthScoreDay == null ? -1 : beltHealthScoreDay.getScore().intValue();
  }

  @Override
  public Integer todayHealthScore(Long id) {
    LocalDateTime todayStart = LocalDate.now().atStartOfDay();
    BeltHealthScoreDay beltHealthScoreDay = baseMapper.selectOne(new LambdaQueryWrapper<BeltHealthScoreDay>()
            .eq(BeltHealthScoreDay::getHealthType, 1)
                    .eq(BeltHealthScoreDay::getBeltId, id)
            .between(BeltHealthScoreDay::getScoreDay, todayStart, todayStart.plusDays(1)));
    return beltHealthScoreDay == null ? -1 : beltHealthScoreDay.getScore().intValue();
  }

  @Override
  public List<BeltMonthlyAverageScoreVO> monthlyAverageScore(Long id, LocalDateTime todayStart) {
    return baseMapper.monthlyAverageScore(id, todayStart);
  }

}